package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.findPage(queryPageBean);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try {
            checkItemService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @Autowired
    private JedisPool jedisPool;


//    @RequestMapping("/findById")
//    public Result findById(Integer id) {
//        try {
//
//            String value = jedisPool.getResource().get(id.toString());
//            if (value == null) {
//                CheckItem checkItem = checkItemService.findById(id);
//                Random random = new Random();
//                int i = random.nextInt(200);
//                if(checkItem==null) {
//                    //插入的值不能为null，而是空字符串，这样判断条件就会走有数据的值，以及它不会一直走 value==null的判断
//                    jedisPool.getResource().setex(id.toString(),60+i , "");
//                } else {
//                    String checkItemJson = JSON.toJSON(checkItem).toString();
//                    jedisPool.getResource().setex(id.toString(),60+i, checkItemJson);
//                }
//                return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
//            }
//
//            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, value);
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckItem> all = checkItemService.findAll();
        if (all != null && all.size() != 0) {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, all);
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}
